import uuid
from datetime import datetime

import pytz as pytz

from extension import db


def get_beijing_time():
    tz = pytz.timezone('Asia/Shanghai')
    return datetime.now(tz)
#关系对象模型  orm用面向对象的思想处理数据库
class Book(db.Model):#使用orm工具创建一个db对象，db.model创建一个数据模型
    __tablename__ = 'book'#数据表名也叫book
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    book_number = db.Column(db.String(255), nullable=False)
    book_name = db.Column(db.String(255), nullable=False)
    book_type = db.Column(db.String(255), nullable=False)
    book_prize = db.Column(db.Float, nullable=False)
    author = db.Column(db.String(255))
    book_publisher = db.Column(db.String(255))
    #数据库初始化
    @staticmethod
    def init_db():
        rets = [
            (1, '001', '活着', '小说', 39.9, '余华', '某某出版社'),
            (2, '002', '三体', '科幻', 99.8, '刘慈欣', '重庆出版社')
        ]
        for ret in rets:
            book = Book()
            book.id = ret[0]
            book.book_number = ret[1]
            book.book_name = ret[2]
            book.book_type = ret[3]
            book.book_prize = ret[4]
            book.author = ret[5]
            book.book_publisher = ret[6]
            db.session.add(book)
        db.session.commit()

#定义模型前先新建数据库
#定义完数据模型记得映射到数据库
class UserModel(db.Model):
    __tablename__ = 'user'
    user_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(255), nullable=True)
    user_img=db.Column(db.String(100),nullable=True,unique=False)#可以为空，不唯一
    OpenID=db.Column(db.String(100),nullable=False,unique=True)
    password = db.Column(db.String(100), nullable=True, unique=False)  # 网页侧使用，小程序用不到
    user_registertime = db.Column(db.DateTime, default=get_beijing_time)

    #数据库初始化方法
    @staticmethod
    def init_db():
        rets = [
            ("admin",
             'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
             'openid'),
        ]
        for ret in rets:
            user = UserModel()
            user.username=ret[0]
            user.user_img=ret[1]
            user.OpenID=ret[2]
            db.session.add(user)
        db.session.commit()
    #其他数据库方法
    def to_dict(self):
        return {
            "user_id": self.user_id,
            "openid": self.OpenID,
            "username": self.username,
            "avatar":self.user_img
        }

class RoomModel(db.Model):
    __tablename__ = 'room'
    roomid = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    is_over = db.Column(db.Boolean, default=False, nullable=False)  # 房间是否结束
    total_score = db.Column(db.Text, default='{}')
    room_createtime = db.Column(db.DateTime, default=get_beijing_time)

    @staticmethod
    def init_db():
        room = RoomModel()
        room.is_over = False
        room.total_score = '{}'
        db.session.add(room)
        db.session.commit()

class RoomUserModel(db.Model):
    __tablename__ = 'room_user'
    room_id = db.Column(db.String(36), db.ForeignKey('room.roomid'), primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), primary_key=True)

class DetailModel(db.Model):
    __tablename__ = 'detail'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    room_id = db.Column(db.String(36), db.ForeignKey('room.roomid'))
    round_number = db.Column(db.Integer, nullable=False)
    scores = db.Column(db.Text, nullable=False)


    created_at = db.Column(db.DateTime, default=get_beijing_time())

    @staticmethod
    def init_db():
        detail = DetailModel()
        detail.room_id = '1'  # 确保 room_id 存在
        detail.round_number = 1
        detail.scores = '{"user1": 10, "user2": 20}'
        db.session.add(detail)
        db.session.commit()
